package org.qiyu.live.account.provider.service.Impl;

import jakarta.annotation.Resource;
import org.idea.qiyu.live.framework.redis.starter.key.AccountProviderCacheKeyBuilder;
import org.qiyu.live.account.provider.service.IAccountTokenService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: aaa
 * @Description: TODO
 * @Author zhangguangyan
 * @Date 2023/08/29/ 20:50:46
 * @Version 1.0
 */
@Service
public class AccountTokenServiceImpl implements IAccountTokenService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private AccountProviderCacheKeyBuilder accountProviderCacheKeyBuilder;

    @Override
    public String createAndSaveLoginToken(Long userId) {
        String tokenKey = UUID.randomUUID().toString();
        String loginTokenKey = accountProviderCacheKeyBuilder.buildUserLoginToken(tokenKey);
        redisTemplate.opsForValue().set(loginTokenKey, String.valueOf(userId), 30, TimeUnit.DAYS);
        return tokenKey;
    }

    @Override
    public Long getUserIdByToken(String tokenKey) {
        String loginTokenKey = accountProviderCacheKeyBuilder.buildUserLoginToken(tokenKey);
        Object value = redisTemplate.opsForValue().get(loginTokenKey);
        if (value == null) {
            return null;
        }
        Object o = redisTemplate.opsForValue().get(loginTokenKey);
        return Long.valueOf(o+"");
    }

}

